package com.javiercp.ayudaapp.preferences;

import java.io.StringWriter;
import java.io.Writer;
import java.util.LinkedList;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.javiercp.ayudaapp.MyConstants;

import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.util.Log;

public class ContactsPreferenceDeflaterSDK5 extends ContactsPreferenceDeflater {

	@Override
	public String deflate(PreferenceCategory pref) {
		LinkedList<ContactData> contactList = new LinkedList<ContactData>();
		
		for (int i=0;i<pref.getPreferenceCount();i++) {
			Preference foo = pref.getPreference(i);
			
			contactList.add(new ContactData(foo.getTitle().toString(), foo.getSummary().toString())); 
		}
		
		ContactDataList list = new ContactDataList(contactList);
		Serializer serializer = new Persister();	
		Writer writer = new StringWriter();
		
		try
		{
			serializer.write(list, writer);
		}
		catch (Exception ex)
		{
			Log.e(MyConstants.LOGTAG, "Error serializing object.", ex);
		}
		
		return writer.toString();
	}

}
